package com.kqkj.dyrule.datagen;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.HashMap;
import java.util.Properties;
import java.util.Random;

public class ActionLogAutoGen {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers","kqkj101:9092,kqkj102:9092,kqkj103:9092");
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        //创建多个线程，并行执行
        genBatch(properties);

    }

    private static void genBatch(Properties props){
        for (int i=0;i<1;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //构建一个kafka生产者客户端
                    KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(props);
                    while(true){
                        LogBean logBean = getLogBean();
                        //将日志对象，转成JSON
                        String log = JSON.toJSONString(logBean);
                        //写入kafka的topic
                        ProducerRecord<String, String> record = new ProducerRecord<>("zenniu_applog", log);
                        kafkaProducer.send(record);
                        try {
                            Thread.sleep(RandomUtils.nextInt(1000, 2000));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }


                    }

                }
            }).start();
        }
    }

    public static LogBean getLogBean(){
        LogBean logBean = new LogBean();
        String account = StringUtils.leftPad(RandomUtils.nextInt(1, 10) + "", 6, "0");
        logBean.setAccount(account);
        logBean.setAppId("cn.doitedu.yinew");
        logBean.setAppVersion("2.5");
        logBean.setCarrier("中国移动");
        // deviceid直接用account
        logBean.setDeviceId(account);
        logBean.setIp(RandomUtils.nextInt(10,12)+"."+RandomUtils.nextInt(20,22)+"."+RandomUtils.nextInt(100,102)+"."+RandomUtils.nextInt(60,66));
        logBean.setLatitude(RandomUtils.nextDouble(10.0, 52.0));
        logBean.setLongitude(RandomUtils.nextDouble(120.0, 160.0));
        logBean.setDeviceType("mi6");
        logBean.setNetType("5G");
        logBean.setOsName("android");
        logBean.setOsVersion("7.5");
        logBean.setReleaseChannel("小米应用市场");
        logBean.setResolution("2048*1024");
        /**
         * 生成事件ID
         */
        //logBean.setEventId(RandomStringUtils.randomAlphabetic(1).toUpperCase());
        Random r = new Random();
        int i1 = r.nextInt(4);
        if (i1 == 0){
            logBean.setEventId("A");
        } else if (i1 == 1){
            logBean.setEventId("C");
        } else if (i1 == 2){
            logBean.setEventId("F");
        } else {
            logBean.setEventId("K");
        }



        HashMap<String, String> properties = new HashMap<String, String>();
        for (int i = 0; i < RandomUtils.nextInt(1, 5); i++) {
            // 生成的属性形如：  p1=v1, p2=v1, p3=v2,p4=v1,..... p10=
            properties.put("p" + RandomUtils.nextInt(1, 11), "v" + RandomUtils.nextInt(1, 3));
        }
        logBean.setProperties(properties);
        logBean.setTimeStamp(System.currentTimeMillis());
        logBean.setSessionId(RandomStringUtils.randomNumeric(10, 10));




        return logBean;
    }
}
